<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class pvdo_model extends CI_Model {

	public function get_vdo($per_page ,$uri ,$result_by ,$category_by ,$mem_id ,$mem_type)
	{
		if($mem_type != 2)
		{
			$this->db->where('vdo.mem_id',$mem_id);
		}
		
		if($this->uri->segment(1) == 'pvdo' || $this->uri->segment(1) == 'pslide')
		{
			$this->db->limit($per_page ,$uri);
			$this->db->order_by($result_by ,'desc');
			if($category_by != 'all')
			{
				$this->db->where('category.category_id',$category_by);
				$this->db->join('category','category.category_id = vdo.category_id','left');
			}
		}		
		$this->db->where('thumb_choose',1);
		$this->db->join('vdo_thumb','vdo_thumb.vdo_id = vdo.vdo_id','left');
		$query = $this->db->get('vdo');
		return $query->result_array();
	}

	public function count_vdo($result_by ,$category_by ,$mem_id ,$mem_type)
	{				
		if($mem_type != 2)
		{
			$this->db->where('vdo.mem_id',$mem_id);
		}
		
		$this->db->order_by($result_by);
		if($category_by != 'all')
		{
			$this->db->where('category.category_id',$category_by);
			$this->db->join('category','category.category_id = vdo.category_id','left');
		}

		$this->db->where('thumb_choose',1);
		$this->db->join('vdo_thumb','vdo_thumb.vdo_id = vdo.vdo_id','left');
		
		$query = $this->db->get('vdo');
		return $query->result_array();
	}

	public function search($input ,$per_page ,$uri ,$result_by ,$category_by ,$mem_id ,$mem_type)
	{	
		if($uri == '')
		{
			$uri = 0;
		}

		$search = $input['search'];

		if($category_by != 'all')
		{	
			if($mem_type != 2)
			{
				$query = $this->db->query("SELECT * FROM (`vdo`) 
				LEFT JOIN `vdo_thumb` ON `vdo_thumb`.`vdo_id` = `vdo`.`vdo_id` 
				WHERE`vdo`.`mem_id` = $mem_id 
				AND  `vdo_thumb`.`thumb_choose` = 1 
				AND (`vdo`.`vdo_name` LIKE '%$search%' OR `vdo`.`vdo_des` LIKE '%$search%' OR `vdo`.`vdo_tag` LIKE '%$search%') 
				GROUP BY `vdo`.`vdo_id` 
				HAVING `vdo`.`category_id` = $category_by
				ORDER BY $result_by 
				desc LIMIT $uri,$per_page");

			} else {

				$query = $this->db->query("SELECT * FROM (`vdo`) 
				LEFT JOIN `vdo_thumb` ON `vdo_thumb`.`vdo_id` = `vdo`.`vdo_id` 
				WHERE  `vdo_thumb`.`thumb_choose` = 1 
				AND (`vdo`.`vdo_name` LIKE '%$search%' OR `vdo`.`vdo_des` LIKE '%$search%' OR `vdo`.`vdo_tag` LIKE '%$search%') 
				GROUP BY `vdo`.`vdo_id` 
				HAVING `vdo`.`category_id` = $category_by
				ORDER BY $result_by 
				desc LIMIT $uri,$per_page");

			}
			

		} else {

			if($mem_type != 2){

				$query = $this->db->query("SELECT * FROM (`vdo`) 
				LEFT JOIN `vdo_thumb` ON `vdo_thumb`.`vdo_id` = `vdo`.`vdo_id` 
				WHERE `vdo`.`mem_id` = $mem_id 
				AND `vdo_thumb`.`thumb_choose` = 1 
				AND (`vdo`.`vdo_name` LIKE '%$search%' OR `vdo`.`vdo_des` LIKE '%$search%' OR `vdo`.`vdo_tag` LIKE '%$search%') 
				GROUP BY `vdo`.`vdo_id` 
				ORDER BY $result_by 
				desc LIMIT $uri,$per_page");

			} else {

				$query = $this->db->query("SELECT * FROM (`vdo`) 
				LEFT JOIN `vdo_thumb` ON `vdo_thumb`.`vdo_id` = `vdo`.`vdo_id` 
				WHERE `vdo_thumb`.`thumb_choose` = 1 
				AND (`vdo`.`vdo_name` LIKE '%$search%' OR `vdo`.`vdo_des` LIKE '%$search%' OR `vdo`.`vdo_tag` LIKE '%$search%') 
				GROUP BY `vdo`.`vdo_id` 
				ORDER BY $result_by 
				desc LIMIT $uri,$per_page");

			}

			
		}

		return $query->result_array();
	}

	public function count_search($input ,$result_by ,$category_by ,$mem_id ,$mem_type)
	{
		$search = $input['search'];

		if($category_by != 'all')
		{	
			if($mem_type != 2)
			{
				$query = $this->db->query("SELECT * FROM (`vdo`) 
				LEFT JOIN `vdo_thumb` ON `vdo_thumb`.`vdo_id` = `vdo`.`vdo_id` 
				WHERE `vdo`.`mem_id` = $mem_id 
				AND `vdo_thumb`.`thumb_choose` = 1 
				AND (`vdo`.`vdo_name` LIKE '%$search%' OR `vdo`.`vdo_des` LIKE '%$search%' OR `vdo`.`vdo_tag` LIKE '%$search%') 
				GROUP BY `vdo`.`vdo_id` 
				HAVING `vdo`.`category_id` = $category_by
				ORDER BY $result_by 
				desc");



			} else {

				$query = $this->db->query("SELECT * FROM (`vdo`) 
				LEFT JOIN `vdo_thumb` ON `vdo_thumb`.`vdo_id` = `vdo`.`vdo_id` 
				WHERE `vdo_thumb`.`thumb_choose` = 1 
				AND (`vdo`.`vdo_name` LIKE '%$search%' OR `vdo`.`vdo_des` LIKE '%$search%' OR `vdo`.`vdo_tag` LIKE '%$search%') 
				GROUP BY `vdo`.`vdo_id` 
				HAVING `vdo`.`category_id` = $category_by
				ORDER BY $result_by 
				desc");

			}
			

		} else {

			if($mem_type != 2)
			{
				$query = $this->db->query("SELECT * FROM (`vdo`) 
				LEFT JOIN `vdo_thumb` ON `vdo_thumb`.`vdo_id` = `vdo`.`vdo_id` 
				WHERE `vdo`.`mem_id` = $mem_id 
				AND `vdo_thumb`.`thumb_choose` = 1 
				AND (`vdo`.`vdo_name` LIKE '%$search%' OR `vdo`.`vdo_des` LIKE '%$search%' OR `vdo`.`vdo_tag` LIKE '%$search%') 
				GROUP BY `vdo`.`vdo_id` 
				ORDER BY $result_by  
				desc");

			} else {

				$query = $this->db->query("SELECT * FROM (`vdo`) 
				LEFT JOIN `vdo_thumb` ON `vdo_thumb`.`vdo_id` = `vdo`.`vdo_id` 
				WHERE `vdo_thumb`.`thumb_choose` = 1 
				AND (`vdo`.`vdo_name` LIKE '%$search%' OR `vdo`.`vdo_des` LIKE '%$search%' OR `vdo`.`vdo_tag` LIKE '%$search%') 
				GROUP BY `vdo`.`vdo_id` 
				ORDER BY $result_by  
				desc");

			}
			
		}

		return $query->result_array();
	}

	public function get_approve($per_page ,$uri ,$result_by ,$category_by ,$mem_id ,$mem_type)
	{	
		if($mem_type != 2)
		{
			$this->db->where('vdo.mem_id',$mem_id);
		}
		
		$this->db->where('vdo.vdo_status',NULL);	
		if($this->uri->segment(1) == 'pvdo' || $this->uri->segment(1) == 'pslide')
		{
			$this->db->limit($per_page ,$uri);
			$this->db->order_by($result_by ,'desc');
			if($category_by != 'all')
			{
				$this->db->where('category.category_id',$category_by);
				$this->db->join('category','category.category_id = vdo.category_id','left');
			}
		}		
		$this->db->where('thumb_choose',1);
		$this->db->join('vdo_thumb','vdo_thumb.vdo_id = vdo.vdo_id','left');
		$query = $this->db->get('vdo');
		return $query->result_array();
	}

	public function count_approve($result_by ,$category_by ,$mem_id ,$mem_type)
	{		
		if($mem_type != 2)
		{
			$this->db->where('vdo.mem_id',$mem_id);
		}

		$this->db->where('vdo.vdo_status',NULL);		
		$this->db->order_by($result_by);
		if($category_by != 'all')
		{
			$this->db->where('category.category_id',$category_by);
			$this->db->join('category','category.category_id = vdo.category_id','left');
		}
 
		$this->db->where('thumb_choose',1);
		$this->db->join('vdo_thumb','vdo_thumb.vdo_id = vdo.vdo_id','left');
		
		$query = $this->db->get('vdo');

		return $query->result_array();
	}

	public function search_approve($input ,$per_page ,$uri ,$result_by ,$category_by ,$mem_id ,$mem_type)
	{	
		if($uri == '')
		{
			$uri = 0;
		}

		$search = $input['search'];

		if($category_by != 'all')
		{	
			if($mem_type != 2)
			{
				$query = $this->db->query("SELECT * FROM (`vdo`) 
				LEFT JOIN `vdo_thumb` ON `vdo_thumb`.`vdo_id` = `vdo`.`vdo_id` 
				WHERE `vdo`.`vdo_status` IS NULL 
				AND `vdo`.`mem_id` = $mem_id 
				AND  `vdo_thumb`.`thumb_choose` = 1 
				AND (`vdo`.`vdo_name` LIKE '%$search%' OR `vdo`.`vdo_des` LIKE '%$search%' OR `vdo`.`vdo_tag` LIKE '%$search%') 
				GROUP BY `vdo`.`vdo_id` 
				HAVING `vdo`.`category_id` = $category_by
				ORDER BY $result_by 
				desc LIMIT $uri,$per_page");

			} else {

				$query = $this->db->query("SELECT * FROM (`vdo`) 
				LEFT JOIN `vdo_thumb` ON `vdo_thumb`.`vdo_id` = `vdo`.`vdo_id` 
				WHERE `vdo`.`vdo_status` IS NULL 
				WHERE  `vdo_thumb`.`thumb_choose` = 1 
				AND (`vdo`.`vdo_name` LIKE '%$search%' OR `vdo`.`vdo_des` LIKE '%$search%' OR `vdo`.`vdo_tag` LIKE '%$search%') 
				GROUP BY `vdo`.`vdo_id` 
				HAVING `vdo`.`category_id` = $category_by
				ORDER BY $result_by 
				desc LIMIT $uri,$per_page");
			}
			

		} else {

			if($mem_type != 2)
			{
				$query = $this->db->query("SELECT * FROM (`vdo`) 
				LEFT JOIN `vdo_thumb` ON `vdo_thumb`.`vdo_id` = `vdo`.`vdo_id` 
				WHERE `vdo`.`vdo_status` IS NULL 
				AND `vdo`.`mem_id` = $mem_id 
				AND `vdo_thumb`.`thumb_choose` = 1 
				AND (`vdo`.`vdo_name` LIKE '%$search%' OR `vdo`.`vdo_des` LIKE '%$search%' OR `vdo`.`vdo_tag` LIKE '%$search%') 
				GROUP BY `vdo`.`vdo_id` 
				ORDER BY $result_by 
				desc LIMIT $uri,$per_page");

			} else {

				$query = $this->db->query("SELECT * FROM (`vdo`) 
				LEFT JOIN `vdo_thumb` ON `vdo_thumb`.`vdo_id` = `vdo`.`vdo_id` 
				WHERE `vdo`.`vdo_status` IS NULL 
				AND `vdo_thumb`.`thumb_choose` = 1 
				AND (`vdo`.`vdo_name` LIKE '%$search%' OR `vdo`.`vdo_des` LIKE '%$search%' OR `vdo`.`vdo_tag` LIKE '%$search%') 
				GROUP BY `vdo`.`vdo_id` 
				ORDER BY $result_by 
				desc LIMIT $uri,$per_page");
			}
			
		}
		return $query->result_array();
	}

	public function count_search_approve($input ,$result_by ,$category_by ,$mem_id ,$mem_type)
	{

		$search = $input['search'];

		if($category_by != 'all')
		{	
			$query = $this->db->query("SELECT * FROM (`vdo`) 
				LEFT JOIN `vdo_thumb` ON `vdo_thumb`.`vdo_id` = `vdo`.`vdo_id` 
				WHERE `vdo`.`vdo_status` IS NULL 
				AND `vdo`.`mem_id` = $mem_id 
				AND `vdo_thumb`.`thumb_choose` = 1 
				AND (`vdo`.`vdo_name` LIKE '%$search%' OR `vdo`.`vdo_des` LIKE '%$search%' OR `vdo`.`vdo_tag` LIKE '%$search%') 
				GROUP BY `vdo`.`vdo_id` 
				HAVING `vdo`.`category_id` = $category_by
				ORDER BY $result_by 
				desc");

		} else {

			if($mem_type != 2)
			{
				$query = $this->db->query("SELECT * FROM (`vdo`) 
				LEFT JOIN `vdo_thumb` ON `vdo_thumb`.`vdo_id` = `vdo`.`vdo_id` 
				WHERE `vdo`.`vdo_status` IS NULL 
				AND `vdo`.`mem_id` = $mem_id 
				AND `vdo_thumb`.`thumb_choose` = 1 
				AND (`vdo`.`vdo_name` LIKE '%$search%' OR `vdo`.`vdo_des` LIKE '%$search%' OR `vdo`.`vdo_tag` LIKE '%$search%') 
				GROUP BY `vdo`.`vdo_id` 
				ORDER BY $result_by 
				desc");

			} else {

				$query = $this->db->query("SELECT * FROM (`vdo`) 
				LEFT JOIN `vdo_thumb` ON `vdo_thumb`.`vdo_id` = `vdo`.`vdo_id` 
				WHERE `vdo`.`vdo_status` IS NULL 
				AND `vdo_thumb`.`thumb_choose` = 1 
				AND (`vdo`.`vdo_name` LIKE '%$search%' OR `vdo`.`vdo_des` LIKE '%$search%' OR `vdo`.`vdo_tag` LIKE '%$search%') 
				GROUP BY `vdo`.`vdo_id` 
				ORDER BY $result_by 
				desc");
			}
			
		}
		return $query->result_array();
	}

	

	public function get_profile($id)
	{
		$this->db->where('vdo.vdo_id',$id);	
		$this->db->where('vdo_thumb.thumb_choose',1);
		$this->db->join('vdo_thumb','vdo_thumb.vdo_id = vdo.vdo_id','left');	
		$this->db->join('server','server.server_id = vdo.server_id','left');
		$this->db->join('member','member.mem_id = vdo.mem_id','left');
		$this->db->join('category','category.category_id = vdo.category_id','left');
		$query = $this->db->get('vdo');
		return $query->result_array();
	}

	public function get_thumb($id)
	{
		$this->db->where('vdo_id',$id);
		$query = $this->db->get('vdo_thumb');
		return $query->result_array();
	}

	public function update_thumb($thumb)
	{
		$this->db->where('thumb_id' ,$thumb['thumb_id']);
		$this->db->update('vdo_thumb' ,$thumb);
	}

	public function add_thumb($input)
	{
		$this->db->insert('vdo_thumb' ,$input);
	}

	public function update_profile($input)
	{
		$this->db->where('vdo_id',$input['vdo_id']);
		$this->db->update('vdo',$input);
	}

	public function redirect($destination_file)
	{
		$this->db->select('vdo_id');
		$this->db->from('vdo');
		$this->db->where('vdo_rawfile',$destination_file);
		$query = $this->db->get();
		return $query->result_array();
	}

	public function get_category()
	{
		$query = $this->db->get('category');
		return $query->result_array();
	}

	public function delete_category($id)
	{
		$this->db->where('category_id',$id);
		$this->db->delete('category');
	}

	public function sum_status($priority)
	{
		$this->db->where('vdo_status',$priority);
		$query = $this->db->get('vdo');
		return $query->result_array();
	}

	public function update_status($id ,$input)
	{
		$this->db->where('vdo_id' ,$id);
		$this->db->update('vdo' ,$input);
	}

	public function report($vdo_id)
	{
		$this->db->where('vdo_id' ,$vdo_id);
		$query = $this->db->get('report_vdo');
		return $query->result_array();
	}

	public function check_change_category($id)
	{
		$this->db->where('category_id' ,$id);
		$this->db->select('vdo_id');
		$query = $this->db->get('vdo');
		return $query->result_array();
	}

	public function change_category($id ,$input)
	{
		$this->db->where('vdo_id' ,$id);
		$this->db->update('vdo' ,$input);
	}

	public function get_server()
	{
		$query = $this->db->get('server');
		return $query->result_array();
	}

}